package com.vcredit.creditcat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.model.ProductItem;
import com.vcredit.creditcat.persistence.base.ProductItemMapper;
import com.vcredit.creditcat.util.Utility;

/**
 * 
 **/
@Service
public class ProductItemService {
	@Autowired
	private ProductItemMapper mapper;

	public void insert(ProductItem obj) {
		mapper.insert(obj);
	}

	public void update(ProductItem obj) {
		mapper.update(obj);
	}

	public Map<Long, ProductItem> findAll() {
		Map<Long, ProductItem> result = new HashMap<Long, ProductItem>();
		List<ProductItem> list = mapper.findAll();
		for (ProductItem obj : list) {
			result.put(obj.getProductId(), obj);
		}
		return result;
	}

	private List<ProductItem> find(ProductItem obj) {
		return mapper.find(obj);
	}

	public ProductItem findByProductId(Long productId) {
		ProductItem productItem = new ProductItem();
		productItem.setProductId(productId);
		List<ProductItem> list = this.find(productItem);
		if(Utility.isNullOrEmpty(list)) {
			productItem.setMinCode(0);
		} else {
			productItem = list.get(0);
		}
		return productItem;
	}
}